package com.cd.utils;

import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cd.utils.UtilCommon;

/**
 *  文件上传
 *@Author zhk
 *@Date 2018-1-15
 **/
public class UpLoadFile {
        
	private static File fileupload; //上传图片
	private static String imageUrl;//图片路径
	private static String attachmentUrl;
	private static String fileRealName;
	private static String fileuploadFileName; 

	/**
	 * 上传文件
	 * @param request
	 * @param response
	 * @return
	 */
	
	public static String uploadFile(HttpServletRequest request, HttpServletResponse response) {
		String extName = "";
		String newFileName = ""; 
		String nowTimeStr = ""; 
		PrintWriter out = null;
		SimpleDateFormat sDateFormat;
		Random r = new Random();
		/*String savePath = ServletActionContext.getServletContext().getRealPath(
				""); */		
		String savePath = request.getSession().getServletContext().getRealPath("");
        savePath = savePath + "/static/uploadFiles/";
		// response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000;
		sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		nowTimeStr = sDateFormat.format(new Date()); 
		if (fileuploadFileName.lastIndexOf(".") >= 0) {
			extName = fileuploadFileName.substring(fileuploadFileName
					.lastIndexOf("."));
		}
		try {
			out = response.getWriter();
			newFileName = nowTimeStr + rannum + extName;			
			//System.out.println(newFileName);
			String filePath = savePath + newFileName;
			filePath = filePath.replace("\\", "/");
			if (UtilCommon.checkIsImage(extName)) {
				FileUtils.copyFile(fileupload, new File(filePath));
				out.print("<font color='red'>" + fileuploadFileName
						+ "上传成功!</font>#" + filePath + "#" + newFileName);
				//System.out.println(fileuploadFileName+"+++++++++++++");
			} else {
				out.print("<font color='red'>上传的文件类型错误，请选择jpg,jpeg,png和gif格式的图片</font>");
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			out.print("上传失败，出错啦!");
		}
		
		return null;
	}
	
	/**
	 * 上传文件描述
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/uploadDescribe")
	public String uploadImageDescribeFile(HttpServletRequest request, HttpServletResponse response) {
		String extName = "";
		String newFileName = ""; 
		String nowTimeStr = ""; 
		PrintWriter out = null;
		SimpleDateFormat sDateFormat;
		Random r = new Random();
		String savePath = request.getSession().getServletContext().getRealPath("");
        savePath = savePath + "/saveimage/uploadImageDescFiles/";
		//HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000;
		sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		nowTimeStr = sDateFormat.format(new Date()); 
		if (fileuploadFileName.lastIndexOf(".") >= 0) {
			extName = fileuploadFileName.substring(fileuploadFileName
					.lastIndexOf("."));
		}
		try {
			out = response.getWriter();
			newFileName = nowTimeStr + rannum + extName;
			
			//System.out.println(newFileName);
			String filePath = savePath + newFileName;
			filePath = filePath.replace("\\", "/");
			if (UtilCommon.checkIsImage(extName)) {
				FileUtils.copyFile(fileupload, new File(filePath));
				out.print("<font color='red'>" + fileuploadFileName
						+ "上传成功!</font>#" + filePath + "#" + newFileName);
				//System.out.println(fileuploadFileName+"+++++++++++++");
			} else {
				out.print("<font color='red'>上传的文件类型错误，请选择jpg,jpeg,png和gif格式的图片</font>");
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			out.print("上传失败，出错啦!");
		}
		return null;
	}
	
	/**
	 * 图片回调显示
	 * @return
	 * @throws Exception
	 */
	public String showImage(HttpServletResponse response) throws Exception {
		System.out.println(imageUrl);
		File file = new File(imageUrl);
		InputStream is = new FileInputStream(file);
		Image image = ImageIO.read(is);
		String imageType = imageUrl.substring(imageUrl.lastIndexOf(".") + 1);
		RenderedImage img = (RenderedImage) image;
		OutputStream out = response.getOutputStream();
		ImageIO.write(img, imageType, out);
		out.flush();
		out.close();
		return null;
	}
	
	
	
}
